<template>
  <div>
    <div class="ibox-content no-border no-padding-bottom">
      <!--搜索栏-->
      <el-col>
        <el-form :inline="true" :model="filterForm" size="mini" label-width="100px" ref="filterForm">
          <el-form-item label="id">
            <el-input v-model="filterForm.id" width="150px" placeholder="精确匹配用户id"></el-input>
          </el-form-item>
          <el-form-item label="APP类型">
            <el-select style="width: 178px" v-model="filterForm.app_type" placeholder="账号状态">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.app_type"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="APP名称">
            <el-input v-model="filterForm.app_name" width="150px" placeholder="APP名称"></el-input>
          </el-form-item>
          <el-form-item label="结算方式">
            <el-select style="width: 178px" v-model="filterForm.final_type" placeholder="账号状态">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.final_type"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="最小贷款金额">
            <el-input v-model="filterForm.min_loan_amount" width="150px" placeholder="最小贷款金额"></el-input>
          </el-form-item>
          <el-form-item label="最大贷款金额">
            <el-input v-model="filterForm.max_loan_amount" width="150px" placeholder="最大贷款金额"></el-input>
          </el-form-item>
          <el-form-item label="最短贷款天数">
            <el-input v-model="filterForm.min_loan_day" width="150px" placeholder="最短贷款天数"></el-input>
          </el-form-item>
          <el-form-item label="最长贷款天数">
            <el-input v-model="filterForm.max_loan_day" width="150px" placeholder="最长贷款天数"></el-input>
          </el-form-item>
          <el-form-item label="首页分类">
            <el-select style="width: 178px" v-model="filterForm.homepage_cate" placeholder="账号状态">
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.homepage_cate"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="送减息卡">
            <el-select style="width: 178px" v-model="filterForm.is_reduce_interest" placeholder="">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.reduce_interest"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="低门槛">
            <el-select style="width: 178px" v-model="filterForm.is_threshold_low" placeholder="">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.threshold_low"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select style="width: 178px" v-model="filterForm.status" placeholder="账号状态">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="key" :key="index"
                         v-for="(item,key,index) in appProductOptions.status"></el-option>
            </el-select>
          </el-form-item>
          <br>
          <el-form-item>
            <el-button @click.native="filterClick" type="primary">查询</el-button>
            <el-button @click.native="resetFilterForm('filterForm')">重置</el-button>
          </el-form-item>
          <div class="hr-line-dashed"></div>
          <br/>
          <el-form-item size="small">
            <el-button @click="multiplSoldOut" type="primary" plain icon="el-icon-bottom">下架产品</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="ibox-content">
      <!--表格-->
      <el-table
        ref="multipleTable"
        :data="arrList"
        size="medium"
        border
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        :max-height="500"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80" sortable fixed="left"></el-table-column>
        <el-table-column prop="app_type" label="app类型"></el-table-column>
        <el-table-column prop="app_name" label="app名称"></el-table-column>
        <el-table-column prop="final_type" label="结算方式"></el-table-column>
        <el-table-column prop="loanMoney" label="贷款金额" width="160">
          <template slot-scope="scope">
            ￥{{scope.row.min_loan_amount}}~￥{{scope.row.max_loan_amount}}
          </template>
        </el-table-column>
        <el-table-column prop="loanTime" label="贷款期限" width="120">
          <template slot-scope="scope">
            {{scope.row.min_loan_day}}天~{{scope.row.max_loan_day}}天
          </template>
        </el-table-column>
        <el-table-column prop="homepage_cate" label="首页分类"></el-table-column>
        <el-table-column prop="is_reduce_interest" label="送减息卡"></el-table-column>
        <el-table-column prop="is_threshold_low" label="低门槛"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status=='上架'" class="green">上架</span>
            <span v-if="scope.row.status=='下架'" class="warning">下架</span>
            <span v-if="scope.row.status=='--'">--</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="130" fixed="right">
          <template slot-scope="scope">
            <el-tooltip @click.native="handleSold(scope.row)" :enterable="false" effect="dark" content="下架"
                        placement="top-start">
              <el-button type="primary" plain size="mini" icon="el-icon-bottom"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页工具条-->
      <div v-show="arrList.length>0" style="margin-top: 18px;text-align: center">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currPage"
          :page-sizes="[20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totalCount">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import {appRackinglist, appProductOptions, soldOut} from "../../api/product";

  export default {
    name: "AppRackingProduct",
    data() {
      return {
        select: '',
        appProductOptions: {},
        arrList: [],
        pageInfo: {
          totalPage: 0,// 总页数
          totalCount: 0,// 总条数
          pageSize: 0,// 每页条数
          currPage: 1// 当前页
        },
        filterForm: {},
        tableLoading: false,
        tableSelectData: [],

      }
    },
    created() {
      this.getAppProductList();
      this.getAppProductOptions();
    },
    methods: {
      selectChange(val) {
        console.log(val)
      },
      addProduct() {
        this.$router.push({name: 'AddProduct'});
      },
      //下架
      handleSold(row) {
        this.$confirm(`确定要下架APP产品"${row.app_name}"吗`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.commonSoldAppMethods(row.id)
        }).catch(() => {

        })
      },
      commonSoldAppMethods(id) {
        soldOut({id: id}).then(res => {
          this.$message.success(res.msg);
          this.getAppProductList();
        }).catch(error => {

        })
      },
      //批量下架
      multiplSoldOut() {
        if (this.tableSelectData.length > 0) {
          let ids = [];
          this.tableSelectData.forEach((item) => {
            ids.push(item.id)
          });
          if (ids.length > 0) {
            this.$confirm('确定要下架APP产品吗', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              center: true
            }).then(() => {
              this.commonSoldAppMethods(ids.toString())
            }).catch(() => {

            })
          }
        } else {
          this.$message.warning('请选择要下架的APP产品')
        }
      },
      //勾选
      handleSelectionChange(data) {
        this.tableSelectData = data;
      },
      getAppProductList() {
        this.tableLoading = true;
        appRackinglist({
          page: this.pageInfo.currPage,
          ...this.filterForm
        }).then(res => {
          this.tableLoading = false;
          const {listData, page} = res.data;
          this.arrList = listData;
          this.pageInfo = page
        }).catch(error => {
          this.tableLoading = false
        })
      },
      //查询
      filterClick() {
        this.pageInfo.currPage = 1;
        this.getAppProductList();
      },
      //重置
      resetFilterForm(form) {
        this.$refs[form].resetFields();
        this.filterForm = {};
        this.pageInfo.currPage = 1;
        this.getAppProductList();
      },
      handleCurrentChange(val) {
        // console.log(val);
        this.pageInfo.currPage = val;
        this.getAppProductList();
      },
      getAppProductOptions() {
        appProductOptions({}).then(res => {
          this.appProductOptions = res.data;
          console.log(this.appProductOptions);
        }).catch(()=>{

        })
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "~element-ui/packages/theme-chalk/src/common/var";

  .red {
    color: $--color-danger;
  }

  .green {
    color: $--color-primary;
  }

  .warning {
    color: $--color-warning;
  }
</style>

<style scoped lang="scss">

</style>
